﻿#include "login.h"
#include"global.h"
#include "ui_login.h"
#include "readersql.h"
Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    ui->setupUi(this);
    flag=false;
    QFont contentFont=QFont("微软雅黑",10);
    ui->registerLabel->setFont(contentFont);
    ui->registerLabel->setStyleSheet("color:#365ac3");
    ui->fogetLabel->setFont(contentFont);
    ui->fogetLabel->setStyleSheet("color:#365ac3");

    dig=new ChangePassword(this);
    connect(ui->registerLabel,&EasyClickLabel::clicked,this,[=](){
            emit sendR();
        });
    connect(ui->fogetLabel,&EasyClickLabel::clicked,this,[=](){
            dig->sildeIn();

    });
    //******搜索框********//
    QAction * usericon=new QAction(this);
    usericon->setIcon(QIcon(":/images/user-circle.png"));
    QAction * passwordLock=new QAction(this);
    passwordLock->setIcon(QIcon(":/images/lock-on.png"));
//    QAction * passwordLock_ON=new QAction(this);
//    passwordLock_ON->setIcon(QIcon(":/images/browse"));
    QAction * passwordLock_OFF=new QAction(this);
    passwordLock_OFF->setIcon(QIcon(":/images/browse-off"));

    ui->user->setPlaceholderText("身份证号:(admin:123)");
    ui->password->setPlaceholderText("密码:(admin:123)");
    ui->user->addAction(usericon,QLineEdit::LeadingPosition);
    ui->password->addAction(passwordLock,QLineEdit::LeadingPosition);
    ui->password->addAction(passwordLock_OFF,QLineEdit::TrailingPosition);

    connect(passwordLock_OFF,&QAction::triggered,this,[=](){
        if(this->On==false){

            ui->password->setEchoMode(QLineEdit::Normal);
            this->On=true;
            passwordLock_OFF->setIcon(QIcon(":/images/browse"));
        }
        else {
            passwordLock_OFF->setIcon(QIcon(":/images/browse-off"));
            ui->password->setEchoMode(QLineEdit::Password);
            this->On=false;
        }

    });

    connect(ui->user,&QLineEdit::returnPressed,this,&Login::processRet);
    connect(ui->password,&QLineEdit::returnPressed,this,&Login::processRet);
    /****************************/
    /*此处编辑 该项目的具体信息*/
    QString str="欢迎来到图书管理系统\n项目作者:\n胡锡睿(组长)\n胡程鹏(前端)\n胡伟峰(后端)\n吕乐天(后端)\n\ngitee:\ngitee.com/max_library";

    ui->introtext->setText(str);

}

void Login::on_btn_login_clicked()
{//数据库查找用户名和密码
    qDebug()<<"previous user"<<global::user;
     global::user = QString(ui->user->text());
     qDebug()<<"user now"<<global::user;
    if(global::user.length()==0)
    {QMessageBox::information(this,tr("提示对话框"),tr("请输入ID！"), QMessageBox::Ok);
     return;}
    QString password_real = global::read->Password(global::user);
    QString password_entered = QString(ui->password->text());
    QSqlQuery sql(global::read->m_db);

    QString strSql = QString("SELECT * FROM Reader WHERE ID=%1").arg(global::user);
    sql.exec(strSql);
    if(sql.next())
    {
        qDebug()<<"previous author"<<global::AUthority;
        global::AUthority=bool(sql.value(9).toUInt());
        global::book_borrowED=sql.value(11).toString();//获取已经借书数量
        global::overtime=sql.value(10).toString();
        qDebug()<<"authority now"<<global::AUthority;
        qDebug()<<"overtime now"<<global::overtime;
        qDebug()<<"book_borrowed now"<<global::book_borrowED;
    }
    flag = bool(sql.value(9).toUInt());
    credit = bool(sql.value(10).toUInt()==0);
 //失败就提示
 if(password_entered != password_real)
 {QMessageBox::information(this,tr("提示对话框"),tr("密码错误，请重新输入！"), QMessageBox::Ok);
  return;}
 else
 //成功进入主界面
 {
 emit sendLoginSuccess();
}
}
Login::~Login()
{
    delete ui;
}


void Login::on_btn_register_clicked()
{emit sendR();

}



void Login::on_minBtn_clicked()
{
    showMinimized();
}


void Login::on_closeBtn_clicked()
{
    exit(0);
}
void Login::processRet(){
    QLineEdit * returnPressedSender=(QLineEdit*)sender();
    if(returnPressedSender==ui->user)
        ui->password->setFocus();
    else if(returnPressedSender==ui->password){
        ui->btn_login->click();
    }

}


